// 初始化：cnpm init -y
// 下载插件：cnpm i gulp gulp-imagemin@7 gulp-babel@7.0.1 babel-core babel-preset-es2015 gulp-clean-css gulp-htmlmin gulp-uglify gulp-autoprefixer gulp-rename gulp-load-plugins gulp-webserver gulp-clean -S
// 导入gulp 并解构
const {src, dest, watch, series, parallel} = require('gulp')
const _  = require('gulp-load-plugins')()
console.log(_);
// 定义任务
// html任务
function handlerHTML(cb){
    src('./project/src/*.html',{
        allowEmpty: true
    })
    .pipe(_.htmlmin({
        collapseWhitespace: true
    }))
    .pipe(dest('./project/dist'))
    cb()
}
// 操作css的任务
function handlerCSS(cb){
    src('./project/src/css/*.css',{
        allowEmpty: true
    })
    .pipe(dest("./project/dist/css"))
    cb()
}
// 操作js的任务
function handlerJS(cb){
    src('./project/src/js/*.js',{
        allowEmpty: true
    })
    .pipe(_.babel({
        presets: ['es2015']
    }))
    .pipe(_.uglify())
    .pipe(dest('./project/dist/js'))
    cb()
}
// 图片处理
// function handlerIMG(cb){
//     src(['./project/src/images/*.*'],{
//         allowEmpty: true
//     })
//     // .pipe(_.imagemin())
//     .pipe(dest('./project/dist/images'))
//     cb()    
// }
// font文件夹
function handlerFont(cb){
    src('./project/src/font/*.*',{
        allowEmpty: true
    })
    .pipe(dest('./project/dist/font'))
    cb()
}
// function handlerLayer(cb){
//     src(['./project/src/js/layer/*.js', './project/src/js/layer/*/*/*.*', './project/src/js/layer/*/*.js', './project/src/js/layer/*/*/*.*'],{
//         allowEmpty: true
//     })
//     .pipe(dest('./project/dist/js/layer'))
//     cb()
// }

// json
function handlerJSON(cb){
    src('./project/src/json/*.json',{
        allowEmpty: true
    })
    .pipe(dest('./project/dist/json'))
    cb()
}
// 网站图片
function handlerICO(cb){
    src('./project/src/favicon.ico',{
        allowEmpty: true
    })
    .pipe(dest('./project/dist'))
    cb()
}
// 清空目标文件夹
function cleanDir(cb){
    src('./project/dist',{
        allowEmpty: true
    })
    .pipe(_.clean())
    cb()
}
// 服务器任务
// 监视任务
function listen(cb){
    /*  console.log(_);
     return  */
    watch('./project/src/*.html', {
        ignoreInitial: false
    }, handlerHTML)
    watch('./project/src/css/*.css', {
        ignoreInitial: false
    }, handlerCSS)
    watch('./project/src/js/*.js', {
        ignoreInitial: false
    }, handlerJS)
    // watch(['./project/src/images/*.*'], {
    //     ignoreInitial: false
    // }, handlerIMG)
    watch('./project/src/json/*.json', {
        ignoreInitial: false
    }, handlerJSON)
    watch('./project/src/font/*.*', {
        ignoreInitial: false
    }, handlerFont)
    // watch('./project/src/js/layer/*', {
    //     ignoreInitial: false
    // }, handlerLayer)
    watch('./project/src/favicon.ico', {
        ignoreInitial: false
    }, handlerICO)
    /* setTimeout(function(){
        src('./project/dist')
        .pipe(_.webserver({
            livereload: true,
            open: true,
        }))
    },5000) */
    cb()
}
// 导出任务
exports.default = series(cleanDir, listen)